
import { _decorator, Component, Node, game, AudioSource, assert, Game, director, log, Label } from 'cc';
import { entityUtil } from './framework/modules/entityUtil';
import { gameState } from './gameState';
import { settingUtil } from './framework/modules/settingUtil';
import { uiManager } from './framework/modules/uiManager';
import { BaseObject } from './framework/modules/baseObject';
import { resourceUtil } from './framework/modules/resourceUtil';
import { audioManager } from './framework/modules/audioManager';
import { appConfig } from './model/appConfig';
import { platformUtil } from './platform/platformUtil';
import { AppConst, EventConst, FormConst } from './const/Const';
import dataUtil from './framework/modules/dataUtil';
import Common from './utils/Common';
import eventUtil from './framework/modules/eventUtil';
import { memoryConfig } from './framework/modules/memoryConfig';
import { platformConf } from './platform/platformConf';
import { BundlesDefine, entityDefine, formDefine, pathDefine } from '../scripts/CustomDefine';
import { DEBUG } from 'cc/env';
import { framingLoadingUtil } from './framework/modules/framingLoadingUtil';
import { dropMoneyModel } from './model/dropMoneyModel';
import { Debug } from './utils/Debug';
const { ccclass, property } = _decorator;

@ccclass('gameEntry')
export class gameEntry extends BaseObject {

    @property({
        type: AudioSource,
        displayName: '音频源'
    })
    public audioSource: Array<AudioSource> = [];

    @property({
        type: appConfig,
        displayName: '广告配置'
    })
    public platformConf: Array<appConfig> = [];

    @property(Label)
    public txtVersion: Label = null;

    onLoad() {
        // if (DEBUG) {
        //     settingUtil.removeAll();
        //     Debug.warn("测试版，清除所有数据");
        // }
        this.init();
        memoryConfig.instance.setKey("online_time", Date.now());
    }


    private init() {
        audioManager.instance.init(this.audioSource);
        let config = this.platformConf.find(item => item.appConst == platformConf.currentApp);
        if (!config) {
            config = this.platformConf.find(item => item.appConst == AppConst.WX);
        }
        platformConf.config = config;
        this.txtVersion.string = `版本号 ${platformConf.config.version}`;
        if (dataUtil.getFrameworkTag() == "") {
            settingUtil.removeAll();
            dataUtil.setFrameworkTag(`${Date.now()}`);
        }

        if (Common.isEmpty(dataUtil.getToken())) {
            //初始花数据
            dataUtil.setIsNew(true);
            dataUtil.setCoin(1000);
            // dataUtil.setCoin(100000);
            dataUtil.setToken(Common.generateUUID());
            // dataUtil.setCurrentLevel(0, false);
            // dataUtil.setCurrentLevel(1, true);
            // dataUtil.setCurrentLevel(2, true);
            // dataUtil.setCurrentLevel(3, true);
            // dataUtil.setCurrentLevel(4, true);
            // dataUtil.setCurrentLevel(5, true);
            // dataUtil.setCurrentLevel(6, true);
            // dataUtil.setCurrentLevel(7, true);
            dataUtil.saveCoin();
        }

        dataUtil.setPusaMoneyTime(Date.now());
        // 游戏显示事件
        game.on(Game.EVENT_SHOW, () => {
            log("Game.EVENT_SHOW");
            director.resume();
            game.resume();
        });

        // 游戏隐藏事件
        game.on(Game.EVENT_HIDE, () => {
            log("Game.EVENT_HIDE");
            dataUtil.setOutlineTime(Date.now());
            if (!DEBUG) {
                this.saveData();
            }

            director.pause();
            game.pause();
        });
        game.frameRate = 60;
        eventUtil.on(EventConst.ON_PLATFORM_SHOW, this, () => {

        });
        eventUtil.on(EventConst.ON_PLATFORM_HIDE, this, () => {
            dataUtil.setOutlineTime(Date.now());
            Debug.log("退到后台，保存数据")
            this.saveData();
        });


        this.saveInterval = memoryConfig.instance.getIntKey("save_interval", 20);

        game.addPersistRootNode(this.node);
    }
    private saveData() {
        this.nowTime = 0;
        framingLoadingUtil.framingLoad(5, (index) => {
            if (index == 0) dataUtil.saveRack();
            if (index == 1) dataUtil.saveCoin();
            // dataUtil.saveLocalStore();
            if (index == 2) dataUtil.saveAutoTaskGoods();
            if (index == 3) dataUtil.saveSales();
            if (index == 4) dataUtil.saveDropMoney();
        });
        Debug.log("返回到后台时，保存数据");
    }

    onEnable() {
        // NOTE: 常驻节点在切场景时会暂停音乐，需要在 onEnable 继续播放
        // 之后需要在引擎侧解决这个问题
    }
    @property
    private saveInterval: number = 20;
    private nowTime: number = 0;
    update(dt) {
        this.nowTime += dt;
        if (this.nowTime >= this.saveInterval) {
            this.saveData();
        }
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
